package com.meishij.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jash.myutils.utils.CommonAdapter;
import com.meishij.R;
import com.meishij.entry.CityEntry;

import java.util.List;

/**
 * Created by Administrator on 2016/10/15.
 * 我是youngKaka 我的心愿是世界和平
 */

public class City_Loc_Adapter extends BaseAdapter{

    private Context context;
    private List<CityEntry.ChildrenBean> list;

    public City_Loc_Adapter(Context context, List<CityEntry.ChildrenBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        QuickIndexViewHolder holder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_shihua_loc_city_view,null);
            holder=new QuickIndexViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder=((QuickIndexViewHolder) convertView.getTag());
        holder.tvname.setText(list.get(position).getText());
        String currentWord = list.get(position).getPinyin().charAt(0) + "";
        if (position > 0) {
            String lastWord = list.get(position - 1).getPinyin().charAt(0) + "";
            if (lastWord.equals(currentWord)) {
                holder.tvfirstName.setVisibility(View.GONE);
            } else {
                holder.tvfirstName.setVisibility(View.VISIBLE);
                holder.tvfirstName.setText(currentWord);
            }
        } else {
            holder.tvfirstName.setVisibility(View.VISIBLE);
            holder.tvfirstName.setText(currentWord);
        }
        return convertView;
    }


    public static class QuickIndexViewHolder extends CommonAdapter.ViewHolder {

        private TextView tvname;
        private TextView tvfirstName;

        public QuickIndexViewHolder(View itemView) {
            super(itemView);
            tvname = (TextView) itemView.findViewById(R.id.name);
            tvfirstName = ((TextView) itemView.findViewById(R.id.first_word));
        }
    }
}
